home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 887 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. From: clamage@Eng.Sun.COM (Steve Clamage)
  2. Message-ID: <4jckdc$ad6@engnews1.Eng.Sun.COM>
  3. X-Original-Date: 27 Mar 1996 23:53:16 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 28 Mar 96 09:26:03 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: static members as members of a metaclass h
  9. Organization: Sun Microsystems Inc.
  10. References: <4jc5lt$doh@arl-news-svc-2.compuserve.com>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMVpbOOEDnX0m9pzZAQELFAF+Php+hlbBb9P/mgA2s5toT8V4cBAOUD9b
  14.     fRsfZK2oeA0hpiCrFuHfEVFMVdw9l8Pr
  15.     =zS80
  16.  
  17. In article doh@arl-news-svc-2.compuserve.com, Philippe Verdy <100105.3120@compu
  18. serve.com> writes:
  19. >Many problems in C++ are related to the need of using some
  20. >static members in the definition of the class.
  21. >
  22. >There are many cases where those static members have to be
  23. >initialized in a proper sane order, as for any instances of
  24. >the class they belong.
  25. >
  26. >Shamely, the current C++ definition does not allow for a clean
  27. >specification of which (and when) classes are constructed
  28. >prior to using them for the construction of other classes.
  29.  
  30. The lack of specification of initialization order across modules is
  31. not due to lack of trying. Many proposals have been made, and none
  32. have been considered adequate. Your proposal for metaclasses is too big
  33. a change to be considered in this round of standardization.
  34.  
  35. I believe a reasonable solution to the problem of initialization of non-local
  36. static data is not to use non-local static data!
  37.  
  38. Instead of a static data object or static class data member, use a 
  39. function with a local static object. Example:
  40.  
  41. T1& global_T1() { static t1(args); return t1; } // instead of T1 global_T1;
  42.  
  43. class C {
  44.     static T2& static_T2(); // instead of T2 static_T2;
  45. };
  46. T2& C::static_T2() { static T2 t2; return t2; }
  47.  
  48. The only way to access static objects t1 or t2 is to call a function.
  49. The first time the access function is called, the object is initialized
  50. then, and only then. You have whatever control over the order of
  51. initialization you need. Typically the order will be implicitly correct,
  52. since no static data can be used until it is initialized.
  53.  
  54. Only two features are missing, compared to non-local static objects:
  55.  
  56. 1. If you never reference an object, it is never initialized.
  57.  
  58. 2. The order of destruction remains unspecified. If that is a problem,
  59. possibly the static objects could be of a type which has a do-nothing
  60. destructor and a "destroy" member function which could be called
  61. explicitly at the proper time.
  62. ---
  63. Steve Clamage, stephen.clamage@eng.sun.com
  64. ---
  65. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  66. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  67. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  68. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  69. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  70.